package top.liujiachen.springboot.shiro.dao.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.liujiachen.springboot.shiro.dao.IPermissionDao;
import top.liujiachen.springboot.shiro.entity.SysPermission;
import top.liujiachen.springboot.shiro.mapper.SysPermissionMapper;

import java.util.List;
import java.util.Optional;

@Service
public class PermissionDao extends ServiceImpl<SysPermissionMapper, SysPermission> implements IPermissionDao {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public List<String> getPermissionsByIds(String ids) {
        List<String> permissions = null;
        if (Optional.ofNullable(ids).isPresent()) {
            try {
                Wrapper wrapper = lambdaQuery().select(SysPermission::getResource).in(SysPermission::getId, ids.split(",")).getWrapper();
                permissions = listObjs(wrapper);
            } catch (Exception e) {
                logger.error("MyBatis-Plus Error: ", e);
            }
        }
        return permissions;
    }
}
